<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父访问子组件的元素</title>
</head>
<body>
<div id="app">
    <div>
        <h1>{{message}}</h1>
        <cpn></cpn>
        <cpn></cpn>
        <cpn ref="aaa"></cpn>
        <button v-on:click="displayChildren">显示子元素</button>
    </div>
</div>
<template id="cpn">
    <div>
        <h1 v-for="(item,index) in obj">{{item}}</h1>
    </div>
</template>
<script src="../../js/vue.js"></script>
<script>
    const cpnc = ({
        template: "#cpn",
        data() {
            return {
                obj: {
                    num1: 1,
                    num2: 2,
                },
                arr: ['刘备', "关羽", "张飞", "诸葛亮"]
            }
        },
        methods: {
            showMessage() {
                console.log("message");
            }
        }
    });
    new Vue({
        el: "#app",
        data: {
            message: "你好，世界",
        },
        components: {
            cpn: cpnc
        },
        methods: {
            displayChildren() {
                //1.$children拿子元素
                /*console.log(this.$children);
                console.log(this.$children[0].arr);
                console.log(this.$children[0].showMessage());*/
                //2.$refs =>对象类型，默认为空对象，需要加ref=""
                console.log(this.$refs.aaa);
            }
        }
    })
</script>
</body>
</html>